package com.ds.smartstudio.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.History;
import com.ds.smartstudio.entity.Type;
import com.ds.smartstudio.entity.User;
import com.ds.smartstudio.service.DeviceService;
import com.ds.smartstudio.service.HistoryService;
import com.ds.smartstudio.service.TypeService;
import com.ds.smartstudio.service.UserService;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.sql.Time;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class DetectionUtil {

    private static UserService staticUserService;

    private static DeviceService staticDeviceService;

    private static HistoryService staticHistoryService;

    private static TypeService staticTypeService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    UserService userService;

    @Autowired
    HistoryService historyService;

    @Autowired
    TypeService typeService;

    public static Date from;

    public static Date to;

    public static int i = 0;

    public static void run() {
        int x = i;
        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
        Runnable runnable = new Runnable() {
            public void run() {
                if (x != i) {
                    ses.shutdown();
                }
                if (belongCalendar(new Date(), from, to)) {
                    List<History> histories = staticHistoryService.selectLatest();
                    List<Device> devices = staticDeviceService.list();
                    for (History history : histories) {
                        for (Device device : devices) {
                            if (device.getDsTypeId().size() > 0) {
                                QueryWrapper<Type> wrapper = new QueryWrapper<>();
                                wrapper.in("ds_type_id", device.getDsTypeId());
/*                                System.out.println("====================================");
                                System.out.println(device.getDsDeviceMac().equals(history.getDsDeviceMac()));
                                System.out.println(staticTypeService.getOne(wrapper).getDsTypeName().equals("switch"));
                                System.out.println(history.getDsHistoryValue().equals("开"));
                                System.out.println(device.getDsDeviceMac());
                                System.out.println(history.getDsDeviceMac());
                                System.out.println(staticTypeService.getOne(wrapper).getDsTypeName());
                                System.out.println(history.getDsHistoryValue());
                                System.out.println("====================================");*/
                                if (device.getDsDeviceMac().equals(history.getDsDeviceMac()) && staticTypeService.getOne(wrapper).getDsTypeName().equals("switch") && history.getDsHistoryValue().equals("开")) {
                                    System.out.println("\n\n警告！警告！\n\n");
                                    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                                    userQueryWrapper.eq("ds_user_name", "root");
                                    String phoneNumber = staticUserService.getOne(userQueryWrapper).getDsUserTel();
                                    CloseableHttpClient httpClient = HttpClients.createDefault();
                                    HttpGet httpGet = new HttpGet("http://localhost:8083/smartstudio/sms/notice/" + phoneNumber);
                                    try {
                                        httpClient.execute(httpGet);
                                        httpClient.close();
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        //立即执行，并且每5秒执行一次
        ses.scheduleAtFixedRate(runnable, 1000, 5000, TimeUnit.MILLISECONDS);
    }


    @PostConstruct
    public void init() {
        staticUserService = userService;
        staticDeviceService = deviceService;
        staticHistoryService = historyService;
        staticTypeService = typeService;
    }

    public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);
        Calendar after = Calendar.getInstance();
        after.setTime(from);
        Calendar before = Calendar.getInstance();
        before.setTime(to);
        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }
}
